package com.qf.framwork.cases.impl;

import com.qf.framwork.cases.ParamsCases;
import org.apache.commons.fileupload.FileItem;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
public class IntegerParamsCases implements ParamsCases {

    @Override
    public Object getParams(Method m, String name, Map<String, FileItem> itemMap, HttpServletRequest request, HttpServletResponse response) {
        if (itemMap != null && itemMap.containsKey(name)) {
            try {
                return Integer.parseInt(itemMap.get(name).getString("utf-8"));
            } catch (UnsupportedEncodingException e) {
                return null;
            }
        }

        String param = request.getParameter(name);
        if (param != null)
            return Integer.parseInt(param);
        else
            return null;
    }
}
